package com.taomu.app.zongchouhuli.moudle.index.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.http.res.ResMainProductListEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductEntity;
import com.taomu.app.zongchouhuli.moudle.index.entity.ProductEntity;
import com.taomu.app.zongchouhuli.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class BaseProductAdp extends RecyclerView.Adapter<BaseProductAdp.BaseProductHolder>{

    private List<ResMainProductListEntity.DataBean> mData;

    private Context mContext;

    private LayoutInflater inflater;

    public BaseProductAdp(Context mContext){
        this.mData = new ArrayList<>();
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
    }

    public void setData(List<ResMainProductListEntity.DataBean> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void addData(List<ResMainProductListEntity.DataBean> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public BaseProductHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_product,parent,false);
        return new BaseProductHolder(view);
    }

    @Override
    public void onBindViewHolder(BaseProductHolder holder, int position) {
        final ResMainProductListEntity.DataBean entity = mData.get(position);
        holder.update(entity);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onItemClick(entity);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class BaseProductHolder extends RecyclerView.ViewHolder{

        private ImageView mIcon;

        private TextView mTitleTv;

        private TextView mIncomeTv;

        private TextView mPriceTv;

        private TextView mLimitTv;

        public BaseProductHolder(View itemView) {
            super(itemView);
            mIcon = itemView.findViewById(R.id.item_product_im);
            mTitleTv = itemView.findViewById(R.id.item_product_title_tv);
            mIncomeTv = itemView.findViewById(R.id.item_product_income_tv);
            mPriceTv = itemView.findViewById(R.id.item_product_price_tv);
            mLimitTv = itemView.findViewById(R.id.item_product_limit_tv);
        }

        public void update(ResMainProductListEntity.DataBean entity){
            Glide.with(mContext).load(entity.getProduct_img_uri()).into(mIcon);
            mTitleTv.setText(entity.getProduct_name());
            mIncomeTv.setText("最高收益"+entity.getMonth_income()+"%");
            mPriceTv.setText("¥"+entity.getProduct_price());
            if(!TextUtils.isEmpty(entity.getEnd_date())) {
                mLimitTv.setText(TimeUtils.timeFomartOnlyDate(Long.valueOf(entity.getEnd_date())) + "为止");
            }
//            if(entity.getProduct_total_num() != 0) {
//                mLimitTv.setText("限量" + String.valueOf(entity.getProduct_total_num()) + "件 " + entity.getEnd_date() + "为止");
//                mLimitTv.setVisibility(View.VISIBLE);
//            }else{
//                mLimitTv.setVisibility(View.INVISIBLE);
//            }
        }
    }

    private ProductAdpListener listener;

    public void setProductAdpListener(ProductAdpListener listener){
        this.listener = listener;
    }

    public interface ProductAdpListener{
        void onItemClick(ResMainProductListEntity.DataBean entity);
    }
}
